<?php namespace App\Http\Controllers\Api\Admin;

use App\AddressInfo;
use App\Banner;
use App\Http\Controllers\Controller;
use App\InvitingAdvert;
use App\JoyInfo;
use App\Notice;
use App\Resume;
use App\SkillLabel;
use Auth;
use App\Classify;
use App\SetTopRule;
use App\Trade;
use App\JobType;
use App\Userinfo;
use App\ProjectInfo;
use App\Admin;
use App\Position;
use App\User;
use Illuminate\Http\Request;
//use spec\Prophecy\Argument\Token\IdenticalValueTokenSpec;

class SystemController extends Controller
{
    public function setBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title   = $request->get('title');
        $linkUrl = $request->get('link_url');
        $sort    = $request->get('sort');
        $imgId   = $request->get('img_id');
        $id      = !is_null($request->get('id'))?$request->get('id'):0;

        $thatBanner = Banner::whereId(trim($id))->first();

        if(!$thatBanner)
            $thatBanner = new Banner();

        if(!is_null($title))$thatBanner->title = $title;
        if(!is_null($linkUrl))$thatBanner->link_url = $linkUrl;
        if(!is_null($sort))$thatBanner->sort = $sort;
        if(!is_null($imgId))$thatBanner->img_id = $imgId;
        $thatBanner->save();

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$thatBanner]);
    }

    public function delBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Banner $thatBanner */
        $thatBanner = Banner::whereId($id)->first();

        if(!$thatBanner)
            return response()->json(["level"=>"error","message"=>"该banner不存在！"]);

        $thatBanner->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatBanner]);
    }

    public function addClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $name = $request->get("name");
        $type = $request->get('pid');
        $sort = $request->get('sort');
        $id      = !is_null($request->get('id'))?$request->get('id'):0;

        $newClassify = classify::whereId(trim($id))->first();

        if(!$newClassify)
            $newClassify = new classify();

        $newClassify->type = $type;
        $newClassify->name = $name;
        $newClassify->sort = $sort;
        $newClassify->save();


        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newClassify]);
    }

    public function delClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Classify $thatClassify */
        $thatClassify = Classify::whereId($id)->first();

        if(!$thatClassify)
            return response()->json(["level"=>"error","message"=>"该分类不存在！"]);

        $thatClassify->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatClassify]);
    }



    public function delPosition(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Classify $thatClassify */
        $thatPosition = Position::whereId($id)->first();

        if(!$thatPosition)
            return response()->json(["level"=>"error","message"=>"该产品不存在！"]);

        $thatPosition->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatPosition]);
    }

    public function addTrade(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $name = $request->get('name');
        $sort = $request->get('sort');
        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $newTrade = Trade::whereId($id)->first();
        if(!$newTrade)
            $newTrade = new Trade();

        if(!is_null($name))$newTrade->name = $name;
        if(!is_null($sort))$newTrade->sort = $sort;
        $newTrade->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newTrade]);
    }

    public function delTrade(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatTrade = Trade::whereId($id)->first();
        if(!$thatTrade)
            return response()->json(['level' => 'error',',message' => '该行业不存在']);

        $thatTrade->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatTrade]);
    }

    public function addJobType(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $name = $request->get('name');
        $trade = $request->get('trade');
        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $newJobType = JobType::whereId($id)->first();
        if(!$newJobType)
            $newJobType = new JobType();

        if(!is_null($name))$newJobType->name = $name;
        if(!is_null($trade))$newJobType->trade_id = $trade;
        $newJobType->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newJobType]);
    }

    public function delJobType(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatJobType = JobType::whereId($id)->first();
        if(!$thatJobType)
            return response()->json(['level' => 'error',',message' => '该行业不存在']);

        $thatJobType->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatJobType]);
    }

    public function setCompanyAuth(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $userId = !is_null($request->get('user_id')) ? $request->get('user_id') : 0;
        $companyAuth = $request->get('company_auth');

        $userInfo = UserInfo::whereUserId($userId)->first();
        if(!$userInfo)
            return response()->json(['level' => 'error','message' => '注册时发生错误']);

        if(!is_null($companyAuth))$userInfo->company_auth = $companyAuth;
        $userInfo->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $userInfo]);
    }

    public function setPersonalAuth(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $userId = !is_null($request->get('user_id')) ? $request->get('user_id') : 0;
        $personalAuth = $request->get('personal_auth');

        $userInfo = Userinfo::whereUserId($userId)->first();
        if(!$userInfo)
            return response()->json(['level' => 'error','message' => '注册时发生错误']);

        if(!is_null($personalAuth))$userInfo->personal_auth = $personalAuth;
        $userInfo->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $userInfo]);
    }

    public function setProject(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title   = $request->get('title');
        $linkUrl = $request->get('link_url');
        $desc    = $request->get('desc');
        $imgId   = $request->get('img_id');
        $content = $request->get('content');
        $type    =$request->get('type');
        $id      = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var ProjectInfo $projectInfo */
        $projectInfo = ProjectInfo::whereId($id)->first();

        if(!$projectInfo)
            $projectInfo=new ProjectInfo();

        $projectInfoType=ProjectInfo::whereType($type)->get();
        if(count($projectInfoType)>=1 )
            return response()->json(['level'=>'error','message' => '只能添加一条！']);


        if(!is_null($title))$projectInfo->title = $title;
        if(!is_null($linkUrl))$projectInfo->link_url = $linkUrl;
        if(!is_null($desc))$projectInfo->desc = $desc;
        if(!is_null($imgId))$projectInfo->img_id = $imgId;
        if(!is_null($content))$projectInfo->content = $content;
        if(!is_null($type))$projectInfo->type = $type;
        $projectInfo->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $projectInfo]);
    }

    public function delProject(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatInfo=ProjectInfo::whereId($id)->first();
        if(!$thatInfo)
            return response()->json(['level' => 'error',',message' => '该id存在']);

        $thatInfo->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatInfo]);
    }

    public function setTopRule(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $num   = $request->get('num');
        $type = $request->get('type');
        $hours    = $request->get('hours');
        $id      = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var SetTopRule $projectInfo */
        $newSetTopRule = SetTopRule::whereId($id)->first();
        if(!$newSetTopRule)
            $newSetTopRule = new SetTopRule();

        if(!is_null($num))$newSetTopRule->num = $num;
        if(!is_null($type))$newSetTopRule->type = $type;
        if(!is_null($hours))$newSetTopRule->hours = $hours;
        $newSetTopRule->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newSetTopRule]);
    }

    public function delTopRule(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0 ;

        $thatSetTopRule = SetTopRule::whereId($id)->first();
        if(!$thatSetTopRule)
            return response()->json(['level' => 'success','message' => '该规则不存在']);

        $thatSetTopRule->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatSetTopRule]);
    }

    public function changeUserStatus(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $status = $request->get('status');
        $id = $request->get('id');

        $changeStatus = User::whereId($id)->first();

        if(!$changeStatus)
            return response()->json(["level"=>"error","message"=>"该用户不存在！"]);

        if($changeStatus->status == 1){
            $changeStatus->status = 0;
        }else{
            $changeStatus->status = 1;
        }

        $changeStatus->save();

        return response()->json(['level'=>'success','message'=>'状态修改成功','data'=>$changeStatus]);
    }
    public function delInvitingAdvert(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatInviting = InvitingAdvert::whereId($id)->first();
        if(!$thatInviting)
            return response()->json(['level' => 'error',',message' => '该招商信息不存在']);

        $thatInviting->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatInviting]);

    }
    public  function setInvitingAdvert(Request $request){

        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;
        $classifyId=$request->get('classifyId');
        $bankuaiId=$request->get('bankuai_id');
        $imgId=$request->get('imgId');
        $province=$request->get('province');
        $city=$request->get('city');
        $area=$request->get('area');
        $addressDetail=$request->get('addressDetail');
        $phone=$request->get('phone');
        $desc=$request->get('desc');
        $advert=InvitingAdvert::whereId($id)->first();
        if(!$advert)
        $advert =new InvitingAdvert();
        if(!is_null($classifyId))$advert->classify_id=$classifyId;
        if(!is_null($imgId))$advert->img_id=$imgId;
        if(!is_null($province))$advert->province=$province;
        if(!is_null($city))$advert->city=$city;
        if(!is_null($area))$advert->area=$area;
        if(!is_null($addressDetail))$advert->address_detail=$addressDetail;
        if(!is_null($phone))$advert->phone=$phone;
        if(!is_null($desc))$advert->desc=$desc;
        if(!is_null($bankuaiId))$advert->inviting_classify_id=$bankuaiId;

        $advert->save();
        return response()->json(['level' => 'success','message' => '设置成功','data' =>$advert]);
    }
    public function delInvitingAdvertClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatClassify =Classify::whereId($id)->first();
        if(!$thatClassify)
            return response()->json(['level' => 'error',',message' => '该分类不存在']);

        $thatClassify->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatClassify]);

    }
    public function setInvitingAdvertClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);
        $id = !is_null($request->get('id')) ? $request->get('id') : 0;
        $name=$request->get('name');
        $type="advert";

        $thatClassify=Classify::whereId($id)->first();

        if (!$thatClassify)
            $thatClassify=new Classify();
        if(!is_null($name))$thatClassify->name=$name;
        if(!is_null($id))$thatClassify->id=$id;
        $thatClassify->type=$type;

        $thatClassify->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $thatClassify]);
    }
    public function delResume(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatResume=Resume::whereId($id)->first();
        if(!$thatResume)
            return response()->json(['level' => 'error',',message' => '该id存在']);

        $thatResume->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatResume]);
    }
    public  function setJoy(Request $request){

        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;
        $imgId=$request->get('imgId');
        $title=$request->get('title');
        $linkUrl=$request->get('linkUrl');
        $desc=$request->get('desc');
        $joy=JoyInfo::whereId($id)->first();

        if(!$joy)
            $joy =new JoyInfo();
        if(!is_null($imgId))$joy->img_id=$imgId;
        if(!is_null($title))$joy->title=$title;
        if(!is_null($linkUrl))$joy->link_url=$linkUrl;
        if(!is_null($desc))$joy->desc=$desc;


        $joy->save();
        return response()->json(['level' => 'success','message' => '设置成功','data' =>$joy]);
    }


    public function delJoy(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatJoy=JoyInfo::whereId($id)->first();
        if(!$thatJoy)
            return response()->json(['level' => 'error',',message' => '该id存在']);

        $thatJoy->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatJoy]);
    }

    public  function  delNotice(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatNotice=Notice::whereId($id)->first();
        if(!$thatNotice)
            return response()->json(['level' => 'error',',message' => '该id存在']);

        $thatNotice->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatNotice]);

    }


    public  function setNotice(Request $request){

        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id       =!is_null($request->get('id')) ? $request->get('id') : 0;
        $title    =$request->get('title');
        $type     =$request->get('type');
        $desc     =$request->get('desc');
        $content  =$request->get('content');


        $notice=Notice::whereId($id)->first();
        if(!$notice)
            $notice =new Notice();
        if (!is_null($title))$notice->title=$title;
        if (!is_null($desc))$notice->desc=$desc;
        if(!is_null($type))$notice->type=$type;
        if(!is_null($content))$notice->content=$content;

        $notice->save();
        return response()->json(['level' => 'success','message' => '设置成功','data' =>$notice]);
    }


    public  function delAddress(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatAddress=AddressInfo::whereId($id)->first();
        if(!$thatAddress)
            return response()->json(['level' => 'error',',message' => '该id存在']);

        $thatAddress->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatAddress]);
    }

    public  function setAddress(Request $request){
/*
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));*/

        $province = $request->get('province');
        $city     = $request->get('city');
        $area     = $request->get('area');
        $town     = $request->get('town');
        $id       = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatAddressInfo = AddressInfo::whereId($id)->first();
        if(!$thatAddressInfo){
            $thatProvince  = AddressInfo::whereProvince($province)->first();
            if(!$thatProvince){
                $thatProvince  = new AddressInfo();
                $thatProvince->province = $province;
                $thatProvince->save();
            }

            $thatCity = AddressInfo::whereCity($city)->wherePid($thatProvince->id)->first();
            if(!$thatCity){
                $thatCity = new AddressInfo();
                $thatCity->pid      = $thatProvince->id;
                $thatCity->province = $province;
                $thatCity->city     = $city;
                $thatCity->save();
            }

            $thatArea = AddressInfo::whereArea($area)->wherePid($thatCity->id)->first();
            if(!$thatArea){
                $thatArea = new AddressInfo();
                $thatArea->pid = $thatCity->id;
                $thatArea->province = $province;
                $thatArea->city     = $city;
                $thatArea->area     = $area;
                $thatArea->save();
            }

            if(!is_null($town) && trim($town) != ''){
                $thatTown = AddressInfo::whereTown($town)->wherePid($thatCity->pid)->first();
                if(!$thatTown){
                    $thatTown = new AddressInfo();
                    $thatTown->pid = $thatArea->id;
                    $thatTown->province = $province;
                    $thatTown->city     = $city;
                    $thatTown->area     = $area;
                    $thatTown->town     = $town;
                    $thatTown->save();
                }
            }
        }else{
            if(!is_null($province))$thatAddressInfo->province=$province;
            if(!is_null($city))$thatAddressInfo->city=$city;
            if(!is_null($area))$thatAddressInfo->area=$area;
            if(!is_null($town))$thatAddressInfo->town=$town;
            $thatAddressInfo->save();
        }

        return response()->json(['level' => 'success','message' => '添加成功','data' => $thatAddressInfo]);
    }

    public function checkAdvent(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = $request->get("id");

        /** @var InvitingAdvert $thatAdvert */
        $thatAdvert = InvitingAdvert::whereId($id)->first();
        if(!$thatAdvert)
            return response()->json(['level'=>'error','message' => '当前内容不存在！']);

        if($thatAdvert->status == 1){
            $thatAdvert->status = 0;
            $thatAdvert->save();
        }else{
            $thatAdvert->status = 1;
            $thatAdvert->save();
        }

        return response()->json(['level'=>'success','message' => '设置成功！']);
    }

    public function setSkillLabel(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $name     = $request->get('name');
        $id       = !is_null($request->get('id')) ? $request->get('id') : 0;

        $newSkillLabel = SkillLabel::whereId($id)->first();
        if(!$newSkillLabel)
            $newSkillLabel = new SkillLabel();

        if(!is_null($newSkillLabel))$newSkillLabel->name             = $name;
        $newSkillLabel->save();

        return response()->json(['level' => 'success','message' => '设置成功','date' => $newSkillLabel]);
    }

    public function delSkillLabel(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thisSkillLabel = SkillLabel::whereId($id)->first();
        if(!$thisSkillLabel)
            return response()->json(['level' => 'error','message' => '该标签不存在']);

        $thisSkillLabel->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thisSkillLabel]);
    }
}